#include "HistogramViewerWidget.hpp"
#include <qpainter.h>
#include <qstring.h>
#include <qpen.h>
#include <sstream>

#define WINDOW_WIDTH 600
#define WINDOW_HEIGHT 600

#define HISTOGRAM_WIDTH 512
#define HISTOGRAM_HEIGHT 512
#define OFFSET_X 10
#define OFFSET_Y 520

    HistogramViewerWidget::HistogramViewerWidget( ImageAnalysis::Histogram* histogram)
    : QWidget(), currentHistogram(histogram)
    {
        resize(WINDOW_WIDTH,WINDOW_HEIGHT);
    }

    HistogramViewerWidget::~HistogramViewerWidget()
    {

    }

    void HistogramViewerWidget::paintEvent ( QPaintEvent * event )
    {
        int indexMaximumValue = currentHistogram->GetIndexMaximumValue( );
        int numberOfValues    = currentHistogram->GetNumberOfValues();

        //currentHistogram->GetNumberOfValues()*2;
        QPainter painter(this);
        QPen pen(Qt::SolidLine);
        pen.setWidth(2);
        pen.setColor(Qt::black);

        painter.setPen(pen);
        painter.setFont(QFont("Arial", 30));





        //painter.drawText(rect(), Qt::AlignCenter, sval);

        // Axe X
        painter.drawLine(OFFSET_X,OFFSET_Y+2,HISTOGRAM_WIDTH,OFFSET_Y+2);

        // Axe Y
        painter.drawLine(OFFSET_X-2,OFFSET_Y+2,OFFSET_X-2,OFFSET_X+2);

        painter.setPen(Qt::red);
        for(int index=0; index < numberOfValues; ++index )
        {
            int value    = (int)(OFFSET_Y - (currentHistogram->GetNumberOfPixel(index)*HISTOGRAM_HEIGHT)/(float)currentHistogram->GetNumberOfPixel(indexMaximumValue));
            int abscisse = OFFSET_X + index*2;
            painter.drawLine(abscisse,OFFSET_Y, abscisse, value );
        }
    }
